import { Component } from '@angular/core';
 import { Router, Params } from '@angular/router';
 import { RegisterService } from '../../service/register.service';

/* jquery插件 */
declare let $:any;

@Component({
    selector: 'establish',
    templateUrl: '../../../views/register/register.html',
    styleUrls: ['../../../css/register/register.css'],
    providers: [
        RegisterService
    ]
})

export class RegisterComponent {
    public phone:any;
    public password:any;
    public email:any;
    public password_confirmation:any;
    public code:any;
    public isPhone :any = /^1(3|4|5|7|8)[0-9]\d{8}$/;
    constructor(
        private  RegisterService : RegisterService,
        private router : Router
    ){
        $.cookie("home","6");
    }

    // 发送注册短信验证码
    private  getMsg(){
        let self : any = this;
        let data : any = {
        mobile: self.phone
        };
        self.RegisterService.sendMsg({
            data: data,
            success: function (result : any) {
                if(result.error === 0){
                    console.log(result.message);
                }else {
                    console.log(result.msg);
                }
            }
        });
    }
    public sMsg(){
        console.log(this.phone);
        if (this.phone == "" || this.phone == null){
            alert("手机号不能为空")
        }else if(this.isPhone.test(this.phone)){
            console.log(this.phone);
            this.getMsg();
        }else alert("手机号格式不对");

    }
    public hideOrShow(){
        let passwordeye = $('.register-eye');
        if(passwordeye.hasClass('invisiblely')){
            passwordeye.removeClass('invisiblely').addClass('visible');//密码可见
            passwordeye.siblings('div').find('input').attr('type','text');
        }else{
            passwordeye.removeClass('visible').addClass('invisiblely');//密码不可见
            passwordeye.siblings('div').find('input').attr('type','password');
        };
    }
    public hideOrShow1(){
        let passwordeye = $('.register-eye1');
        if(passwordeye.hasClass('invisiblely1')){
            passwordeye.removeClass('invisiblely1').addClass('visible1');//密码可见
            passwordeye.siblings('div').find('input').attr('type','text');
        }else{
            passwordeye.removeClass('visible1').addClass('invisiblely1');//密码不可见
            passwordeye.siblings('div').find('input').attr('type','password');
        };
    }
    // 注册页面信息
    private  uRegister(){
        let self : any = this;
        let data : any = {
            code: self.code,
            password: self.password,
            email: self.email,
            password_confirmation: self.password_confirmation,
            mobile: self.phone
        };
        self.RegisterService.register({
            data: data,
            success: function (result : any) {
                if(result.error === 0){
                    self.router.navigate(['/login']);
                    alert("注册成功")
                }else {
                    alert(result.msg);
                }
            }
        });

    }
    public submit(){
        this.uRegister();
    }
}

